package com.aoxin.controller.opm;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.dto.SelectDto;
import com.aoxin.model.opm.SlfClassroom;
import com.aoxin.service.inter.opm.ClassroomService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;

/**
 * 教室Controller
 * 
 * @author Melody
 * @date 2018年3月20日 下午5:50:15
 */
@Controller
@ResponseBody
@RequestMapping("/web/classroom")
public class ClassroomController {

	@Autowired
	private ClassroomService classroomService;

	/**
	 * 添加教室
	 * 
	 * @param classroom
	 *            教室
	 */
	@RequestMapping("/insertClassroom")
	@LogInfo(content = "教室", operateType = OperateType.INSERT)
	public AjaxRes insertClassroom(SlfClassroom classroom, AjaxRes ajaxRes) {
		try {
			Integer classroomId = this.classroomService.insertSlfClassroom(classroom);
			ajaxRes.setSucceed(classroomId,Const.SAVE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 删除教室
	 * 
	 * @param classroomId
	 *            教室ID
	 */
	@RequestMapping("/deleteClassroom")
	@LogInfo(content = "教室", operateType = OperateType.DELETE)
	public AjaxRes deleteClassroom(@RequestParam("classroomId") Integer classroomId, AjaxRes ajaxRes) {
		try {

			this.classroomService.deleteSlfClassroom(classroomId);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 批量删除
	 * 
	 * @param classroomIds
	 *            教室ID集合
	 */
	@RequestMapping("/deleteClassrooms")
	@LogInfo(content = "教室", operateType = OperateType.DELETE)
	public AjaxRes deleteClassrooms(@RequestParam("classroomIds") List<Integer> classroomIds, AjaxRes ajaxRes) {
		try {
			this.classroomService.deleteSlfClassroomByIds(classroomIds);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改教室
	 * 
	 * @param classroomIds
	 *            教室ID集合
	 */
	@RequestMapping("/updateClassroom")
	@LogInfo(content = "教室", operateType = OperateType.UPDATE)
	public AjaxRes updateClassroom(@Valid SlfClassroom classroom,BindingResult bindingResult, AjaxRes ajaxRes) {
		try {
			this.classroomService.updateSlfClassroom(classroom);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改教室状态
	 * 
	 * @param classroomIds
	 *            教室ID集合
	 */
	@RequestMapping("/updateClassroomStates")
	@LogInfo(content = "教室", operateType = OperateType.UPDATE)
	public AjaxRes updateClassroomStates(@RequestParam("classroomIds") List<Integer> classroomIds, @RequestParam("state") Integer state, AjaxRes ajaxRes) {
		try {
			this.classroomService.updateSlfClassroomStates(classroomIds, state);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 根据教室ID查询教室
	 * 
	 * @param classroomId
	 *            教室ID
	 * @return 教室
	 */
	@RequestMapping("/getClassroom")
	@LogInfo(content = "教室", operateType = OperateType.QUERY)
	public AjaxRes getClassroom(@RequestParam("classroomId") Integer classroomId, AjaxRes ajaxRes) {
		try {
			SlfClassroom slfClassroom = this.classroomService.getSlfClassroom(classroomId);
			ajaxRes.setSucceed(slfClassroom, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询教室
	 * 
	 * @param map
	 *            查询条件
	 * @return 教室集合
	 */
	@RequestMapping("/getClassroomList")
	public AjaxRes getClassroomList(@RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfClassroom> slfClassrooms = this.classroomService.getSlfClassroomList(map);
			ajaxRes.setSucceed(slfClassrooms, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 分页查询教室
	 * 
	 * @param map
	 *            条件集合
	 * @return 分页对象
	 */
	@RequestMapping("/slicedQueryClassroom")
	public AjaxRes slicedQueryClassroom(@RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfClassroom> slfClassrooms = this.classroomService.getSlfClassroomList(map);
			ajaxRes.setSucceed(slfClassrooms, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 获取下一排序号
	 * 
	 * @param schoolId
	 *            学校ID
	 * @return 排序号
	 */
	@RequestMapping("/getNextSequence")
	public AjaxRes getNextSequence(@RequestParam(value = "schoolId", required = false) Integer schoolId, AjaxRes ajaxRes) {
		try {
			Long sequence = this.classroomService.getNextSequence(schoolId);
			ajaxRes.setSucceed(sequence, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 获取教室下拉树
	 */
	@RequestMapping("/getClassroomListBySchoolId")
	public AjaxRes getClassroomListBySchoolId(AjaxRes ajaxRes) {
		try {
			List<SelectDto> selectDtos = this.classroomService.getListBySchoolId();
			ajaxRes.setSucceed(selectDtos, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 添加教室
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/insertRoom")
	public AjaxRes insertRoom(AjaxRes ajaxRes,SlfClassroom s){
		try {
			this.classroomService.insertRoom(s);
			ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
		}catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

}
